package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.StuQueryParam;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
   @Autowired
   private StudentMapper studentMapper;
    /**
     * 使用pagehelper插件实现条件分页查询学生信息
     *
     * @return
     */
    public PageResult<Student> list(StuQueryParam stuQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(stuQueryParam.getPage(),stuQueryParam.getPageSize());
        //2.执行查询
        List<Student> list = studentMapper.list(stuQueryParam);
        //3.解析查询结果，并封装
        Page<Student> p = (Page<Student>) list;//获取分页查询结果
        return new PageResult<Student>(p.getTotal(),p.getResult());
    }

    /**
     * 添加学员
     * @param student
     */
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.save(student);
    }

    /**
     * 根据id查询学员信息
     * @param id
     * @return
     */
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    /**
     * 修改学员信息
     * @param student
     */
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 批量删除学员信息
     * @param ids
     */
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    /**
     * 学员违纪处理，违纪处理一次，需要将违纪次数+1，违纪扣分+前端输入的分数。
     * @param id
     * @param score
     */
    public void violation(Integer id, Short score) {
        studentMapper.violation(id,score);
    }
}
